package twentytwentyone_3;

/**
 * @Author: Zahi
 * @Description:
 * @Date Created in 2021-03-15 13:26
 * @Modified By:
 */
public class MatrixReshape_566 {
    public static void main(String[] args){

    }

    public static int[][] matrixReshape(int[][] nums,int r,int c){
        // 无法转换直接返回
        if (r * c != nums.length * nums[0].length){
            return nums;
        }
        int[][] result =  new int[r][c];
        int first = 0;
        int second = 0;
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j < nums[0].length; j++) {
                // 遍历原二维数组，向新数组中添加元素
                result[first][second] = nums[i][j];
                // 如果second和c相等则说明已经添加到了最后，需要换行
                second++;
                if (second == c){
                    second = 0;
                    first++;
                }
            }
        }
        return result;
    }
}
